<template>
  <div class="app-container">
    <el-form  v-hasPermi="['pay:ifconfig:edit']" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="通道提供商" prop="ifGroup">
        <el-input
          v-model="queryParams.if_group"
          placeholder="请输入通道提供商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder=""
          clearable
        >
          <el-option
            v-for="dict in dict.type.paysystem_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['pay:ifconfig:edit']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['pay:ifconfig:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['pay:ifconfig:edit']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->
  <template v-for="arr in ifconfigList">
    <el-row>
      <el-col :span="2" style="min-height: 1px"></el-col>
      <el-col :span="5" v-for="d in arr" style="max-width: 400px;padding: 20px">
        <template v-if="d.id">
          <el-card :body-style="{ padding: '0px',height:'320px'}">
            <template v-if="d.ifType == 1">
              <div class="imagediv" v-if="d.ifGroup == 'bison'" style="background-color: #FFFFFF">
                <img  src="@/assets/logo/bisonlogo.png" data-v-2b49afcc="" style="height: 60px;">
              </div>
              <div class="imagediv" v-else-if="d.ifGroup == 'sunpay'" style="background-color: #FFFFFF">
                <img  src="@/assets/logo/sunpaylogo.png" data-v-2b49afcc="" style="height: 60px;">
              </div>
              <div class="imagediv" v-else >
                <img src="http://jeequan.oss-cn-beijing.aliyuncs.com/jeepay/img/shengpay.png" data-v-2b49afcc="" style="height: 60px;">
              </div>
              <div style="padding: 14px;text-align: center">
                <span>通道id：{{d.id}}</span>
              </div>
              <div style="padding: 14px;text-align: center">
                <span v-if="checkPermi(['pay:ifconfig:edit'])">{{d.ifGroup}}-{{d.ifName}}</span>
                <span v-else>{{d.ifName}}</span>
              </div>
            </template>
            <template v-if="d.ifType == 0">
              <div class="imagediv">
                <img src="http://jeequan.oss-cn-beijing.aliyuncs.com/jeepay/img/shengpay.png" data-v-2b49afcc="" style="height: 60px;">
              </div>
              <div style="padding: 14px;text-align: center">
                <span>通道id：{{d.id}}</span>
              </div>
              <div style="padding: 14px;text-align: center">
                <span v-if="checkPermi(['pay:ifconfig:edit'])">{{d.ifGroup}}-{{d.ifName}}({{d.platCurrency}})</span>
                <span v-else>{{d.ifName}}({{d.currency}})</span>
              </div>
              <div style="padding: 14px;text-align: center">
                <span >费率：{{d.ifRate}}% 代付费率：{{d.ifPayoutRate}}+{{d.ifPayoutSingle}}/笔</span>
              </div>
            </template>

            <div  v-hasPermi="['pay:ifconfig:edit']" style="padding: 14px;text-align: center;border-top: 1px solid #F0F2F5;">
              <el-button type="text"  @click="handleUpdate(d)">修改</el-button>
              <el-button type="text" style="margin-left: 80px" @click="testOrder(d)" v-if="d.ifType == 0">测试</el-button>
              <el-button type="text" style="margin-left: 80px;color: red"  @click="handleDelete(d)">删除</el-button>
            </div>
          </el-card>
        </template>
        <template v-else>
          <el-card :body-style="{ padding: '0px',height:'300px'}">
            <div class="addNewIfDiv"   @click="handleAdd">
              <img src="//jeequan.oss-cn-beijing.aliyuncs.com/jeepay/cdn/s.jeepay.com/manager/assets/add-icon.bdbbe2d3.svg" alt="add-icon" class="jeepay-card-add-icon" data-v-5daa57b4="">
              <div style="padding-top: 5px;font-size: 16px;color: #00000059;"> 新建支付接口</div>
            </div>

          </el-card>
        </template>

      </el-col>
      <el-col :span="2" style="min-height: 1px"></el-col>
    </el-row>
  </template>

    <!-- 添加或修改支付接口配置参数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="通道提供商" prop="ifGroup">
          <el-input v-model="form.ifGroup" placeholder="请输入提供商分组" />
        </el-form-item>
        <el-form-item label="接口名" prop="ifName">
          <el-input v-model="form.ifName" placeholder="请输入接口名" />
        </el-form-item>
        <el-form-item label="支付接口代码" prop="ifCode">
          <el-input v-model="form.ifCode" placeholder="请输入支付接口代码" />
        </el-form-item>
        <el-form-item label="接口配置参数" prop="ifParams">
          <el-input v-model="form.ifParams" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item prop="ifParams" label="通道类型" size="mini">
          <el-radio-group v-model="form.ifType">
            <el-radio :label="0">普通通道</el-radio>
            <el-radio :label="1">VA通道</el-radio>
          </el-radio-group>
        </el-form-item>
        <template v-if="form.ifType === 0">
          <el-form-item label="支付接口费率" prop="ifRate">
            <el-input v-model="form.ifRate" placeholder="请输入支付接口费率" />
          </el-form-item>
          <el-form-item label="代付接口费率" prop="ifRate">
            <el-input v-model="form.ifPayoutRate" placeholder="请输入代付费率" />
          </el-form-item>
          <el-form-item label="代付单笔费用" prop="ifRate">
            <el-input v-model="form.ifPayoutSingle" placeholder="请输入代付单笔费用" />
          </el-form-item>
          <el-form-item label="币种" prop="currency">
            <el-input v-model="form.currency" placeholder="请输入币种" />
          </el-form-item>
          <el-form-item label="平台币种" prop="platCurrency">
            <el-input v-model="form.platCurrency" placeholder="请输入平台币种" />
          </el-form-item>
        </template>

        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.paysystem_state"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIfconfig, getIfconfig, delIfconfig, addIfconfig, updateIfconfig, test } from "@/api/fusion/payifconfig";
import {checkPermi} from "../../../../utils/permission";

export default {
  dicts: ['paysystem_state'],
  name: "Ifconfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付接口配置参数表格数据
      ifconfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        isAsc:'desc',
        orderByColumn:'id',
        pageNum: 1,
        pageSize: 10,
        ifGroup: null,
        ifName: null,
        ifCode: null,
        currency: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ifName: [
          { required: true, message: "接口名不能为空", trigger: "blur" }
        ],
        ifCode: [
          { required: true, message: "支付接口代码不能为空", trigger: "blur" }
        ],
        ifParams: [
          { required: true, message: "接口配置参数,json字符串不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态: 0-停用, 1-启用不能为空", trigger: "blur" }
        ],
        createdTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatedTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermi,
    /** 查询支付接口配置参数列表 */
    getList() {
      this.loading = true;
      listIfconfig(this.queryParams).then(response => {
        let temp =  response.rows;
        let splitNum = 4
        this.ifconfigList = []
        let tempArr = []
        for(let i = 0; i < temp.length; i++) {
          if(i % splitNum === 0) {
            tempArr = []
            this.ifconfigList.push(tempArr)
          }
          tempArr.push(temp[i])
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ifGroup: null,
        ifName: null,
        ifCode: null,
        ifParams: null,
        ifRate: null,
        currency: null,
        state: null,
        remark: null,
        createdTime: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付接口配置参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIfconfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改支付接口配置参数";
      });
    },
    testOrder(row) {
      const id = row.id
      test(id).then(response => {
        let d = response.data;
        debugger
        d = JSON.parse(d);
        if(d.code == 0) {
          window.open(d.payData)
        } else {
          this.$modal.msgError(d.msg);
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateIfconfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIfconfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除支付接口配置参数为"' + row.ifName + '"的数据项？').then(function() {
        return delIfconfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pay/ifconfig/export', {
        ...this.queryParams
      }, `ifconfig_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.addNewIfDiv {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 2px dashed rgba(0, 0, 0, .15);
  background: rgba(0, 0, 0, .03);
  border-radius: 6px;
  box-sizing: border-box;
  cursor: pointer;
  height: 100%;
}
.imagediv {
  background-color: rgb(24, 131, 247);
  height: 120px;
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

</style>

